#include "common.h"




void  com_Exit(){
	PRTLOG("close dbs  will restart sysMgnt in 1 seconds");
    sleep(1);
    exit(0);
}
long com_fileSize(const char *filename){
	FILE * pFile;
	long size=0;
	if(filename==NULL){
		size=0;
	}else{
		pFile = fopen (filename,"rb");
		if (pFile==NULL){
			size=0;
		}else{
			fseek (pFile, 0, SEEK_END);
			size=ftell(pFile);
			fclose(pFile);
		}
	}
	return size;
}

static void sig_reboot(int sig){
	PRTLOG("sysMgn Get signal %d",sig);
	if(sig == SIGTERM || sig == SIGKILL){
		PRTLOG("SIGTERM or SIGKILL");
	}
}
static void sig_SIGSEGV(int signo)
{
	void *array[10];
	int size,i;
	char **strings;
	size = backtrace (array, 10);
	strings = backtrace_symbols (array, size);
	PRTLOG("sysMgn Get signal %d:Get %d stack frames.\n",signo,size);
	for (i = 0; i < size; i++)
		PRTLOG("%s\n", strings[i]);
	free (strings);
	com_Exit();
}

void com_initSignals(){
	 signal(SIGTERM,sig_reboot);
	 signal(SIGKILL,sig_reboot);
	 signal(SIGSEGV,sig_SIGSEGV);
}


typedef struct _cpuRateInfo{
   char name[32];
   unsigned int user;
   unsigned int nice;
   unsigned int sys;
   unsigned int idle;
}CPURateInfo;

typedef struct _memRateInfo{
   unsigned int total;
   unsigned int free;
   unsigned int avaliable;
}memRateInfo;


static int getProcessPidByName(const char *proc_name){
      FILE *fp;
      char buf[100];
      char cmd[200] = {'\0'};
      int pid = -1;
      sprintf(cmd, "pidof %s", proc_name);
      if((fp = popen(cmd, "r")) != NULL)
      {
          if(fgets(buf, 255, fp) != NULL)
          {
              pid = atoi(buf);
          }
      }
      pclose(fp);
      return pid;
}

static int getCpuInfo(char *file,CPURateInfo * info){
  FILE * fp=NULL;
  fp=fopen(file,"r");
  if(fp == NULL){
      printf("open %s fail\n",file);
      return -1;
  }else{
     char data[1024]={'\0'};
     fgets(data,1024,fp);
     sscanf(data,"%s %u %u %u %u",info->name,&info->user,&info->nice,&info->sys,&info->idle);
     fclose(fp);
  }
  return 0;
}

int com_getCPURate(){
	unsigned int rate=0;
	CPURateInfo info1,info2;
	memset(&info1,0,sizeof(CPURateInfo));
	memset(&info2,0,sizeof(CPURateInfo));
	getCpuInfo("/proc/stat",&info1);
	usleep(300000);
	getCpuInfo("/proc/stat",&info2);
	unsigned int total1=info1.user+info1.sys+info1.nice+info1.idle;   
	unsigned int total2=info2.user+info2.sys+info2.nice+info2.idle;
	unsigned int cha = (total2-info2.idle) -(total1-info1.idle);
	float shang = (cha*1.0)/(total2-total1);
	rate = (unsigned int)(100.0*shang);
	return rate;
}


/*
  process=NULL:memrate
  process != NULL:process use mem
*/
int com_getMemRate(char *process){
  unsigned int memRate=0;
  memRateInfo info;
  int pid=-1;
  char fname[512]={'\0'};
  if(process== NULL)
      sprintf(fname,"%s","/proc/meminfo");
  else{
     pid =getProcessPidByName(process);
     if(pid != -1){
       sprintf(fname,"/proc/%d/status",pid);
     }else{
       sprintf(fname,"FAIL");
     }
  }
  FILE * fp=NULL;
  if(strlen(fname)>5){
      fp=fopen(fname,"r");
      if(fp == NULL){
          printf("open %s fail\n",fname);
          return -1;
      }else{
        char data[1024]={'\0'};
        if(process == NULL){
          fgets(data,1024,fp);
          char name[128]={'\0'};
          char unit[128]={'\0'};
          sscanf(data,"%s %u %s",name,&info.total,unit);          
		  fgets(data,1024,fp);
          sscanf(data,"%s %u %s",name,&info.free,unit);
          info.free = info.free/1024;
          info.total = info.total/1024;
		  memRate = (unsigned int)((100.0)*(1-(1.0*info.free)/info.total));
        //   printf("MEM_Usage:%u\%\tDetail:free:%uMB  avaliable:%uMB  total:%uMB \n",(unsigned int)((100.0)*(1-(1.0*info.avaliable)/info.total)),info.free,info.avaliable,info.total); 
        }else{
          char name[128]={'\0'};
		  int i=0;
		  for(i=0;i<14;i++){
        		fgets (data, sizeof(data),fp);
           }//读到第15行
          fgets(data,1024,fp);
          sscanf(data,"%s %u",name,&memRate);
        //   printf("%s:\t%dMB\n",process,VmRss*4/1024);
        }
        fclose(fp);
      }
  }
  return memRate;
}

int com_getFanSpeed(){
	unsigned int speed=0;
	return speed;
}


int com_getCPUTemp(){
	unsigned int temp=40;
	return temp;
}


time_t com_localTimeStr2Int(const char * ts){
	struct tm t;
	sscanf(ts,"%04d-%02d-%02d %02d:%02d:%02d",&t.tm_year,&t.tm_mon,&t.tm_mday,&t.tm_hour,&t.tm_min,&t.tm_sec);
	t.tm_year -= 1900;
	t.tm_mon -= 1;
 	return mktime(&t);
}


void com_localTimeInt2Str(time_t ti,char *dst){
    struct tm *ot;
	//ot = gmtime(&t_int);
    ot = localtime(&ti);
    sprintf(dst,"%4d-%02d-%02d %02d:%02d:%02d\n",1900+ot->tm_year,1+ot->tm_mon,ot->tm_mday,ot->tm_hour,ot->tm_min,ot->tm_sec);
}
